import request from '@/utils/request'
import type {
    CreateDeliveryOrderRequest,
    CreateDeliveryOrderResponse,
    GetOrdersRequest,
    GetOrdersResponse,
    UpdateOrderStatusRequest,
    UpdateOrderStatusResponse,
    LocationInfo,
    AdminStatsResponse
} from './models'

// 配送订单相关API接口

/**
 * 创建配送订单
 * @param data 订单创建数据
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const createDeliveryOrder = (data: CreateDeliveryOrderRequest): Promise<CreateDeliveryOrderResponse> => {
    return request.post('/delivery/orders/create', data)
}

/**
 * 获取订单列表
 * @param params 查询参数
 * @returns Promise<GetOrdersResponse>
 */
export const getDeliveryOrders = (params?: GetOrdersRequest): Promise<GetOrdersResponse> => {
    return request.get('/delivery/orders/list', { params })
}

/**
 * 获取单个订单详情
 * @param orderId 订单ID
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const getDeliveryOrderDetail = (orderId: string): Promise<CreateDeliveryOrderResponse> => {
    return request.get(`/delivery/orders/${orderId}`)
}

/**
 * 更新订单状态
 * @param data 更新数据
 * @returns Promise<UpdateOrderStatusResponse>
 */
export const updateOrderStatus = (data: UpdateOrderStatusRequest): Promise<UpdateOrderStatusResponse> => {
    return request.put('/delivery/orders/status', data)
}

/**
 * 取消订单
 * @param orderId 订单ID
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const cancelDeliveryOrder = (orderId: string): Promise<CreateDeliveryOrderResponse> => {
    return request.put(`/delivery/orders/${orderId}/cancel`)
}

/**
 * 获取快送员可接订单列表
 * @param params 查询参数
 * @returns Promise<GetOrdersResponse>
 */
export const getAvailableOrders = (params?: GetOrdersRequest): Promise<GetOrdersResponse> => {
    return request.get('/delivery/available-orders', { params })
}

/**
 * 快送员接单
 * @param orderId 订单ID
 * @param deliveryPersonnel 快送员信息
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const acceptOrder = (orderId: string, deliveryPersonnel: string): Promise<CreateDeliveryOrderResponse> => {
    return request.post(`/delivery/orders/${orderId}/accept`, { deliveryPersonnel })
}

/**
 * 批量接单
 * @param orderIds 订单ID列表
 * @param deliveryPersonnel 快送员信息
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const acceptOrdersBatch = (orderIds: string[], deliveryPersonnel: string): Promise<CreateDeliveryOrderResponse> => {
    return request.post('/delivery/orders/accept/batch', { orderIds, deliveryPersonnel })
}

/**
 * 完成配送
 * @param orderId 订单ID
 * @param notes 完成备注
 * @returns Promise<CreateDeliveryOrderResponse>
 */
export const completeDelivery = (orderId: string, notes?: string): Promise<CreateDeliveryOrderResponse> => {
    return request.post(`/delivery/orders/${orderId}/complete`, { notes })
}

/**
 * 获取地点列表
 * @param isPickupPoint 是否只获取取货点
 * @returns Promise<{ success: boolean; message: string; data?: LocationInfo[] }>
 */
export const getLocationList = (isPickupPoint?: boolean): Promise<{ success: boolean; message: string; data?: LocationInfo[] }> => {
    const params = isPickupPoint !== undefined ? { isPickupPoint } : {}
    return request.get('/delivery/locations', { params })
}

/**
 * 获取管理员统计数据
 * @returns Promise<AdminStatsResponse>
 */
export const getAdminStats = (): Promise<AdminStatsResponse> => {
    return request.get('/delivery/admin/stats')
}

/**
 * 获取订单列表
 * @param params 查询参数
 * @returns Promise<GetOrdersResponse>
 */
export const getAdminDeliveryOrders = (params?: GetOrdersRequest): Promise<GetOrdersResponse> => {
    return request.get('/delivery/admin/orders/list', { params })
}

/**
 * 获取起点到终点的订单列表
 * @param params 查询参数
 * @returns Promise<GetOrdersResponse>
 */
export const getStartToEndOrders = (params?: GetOrdersRequest): Promise<GetOrdersResponse> => {
    return request.get('/delivery/orders/destination', { params })
}
